home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / template / BaseMath.h next >
C/C++ Source or Header  |  2000-09-11  |  4KB  |  184 lines

  1. // Copyright (C) 2000  Sean Cavanaugh
  2. // This file is licensed under the terms of the Lesser GNU Public License
  3. // (see LPGL.txt, or http://www.gnu.org/copyleft/lesser.txt)
  4.  
  5. #ifndef BASEMATH_H__
  6. #define BASEMATH_H__
  7.  
  8. #if _MSC_VER >= 1000
  9. #pragma once
  10. #endif // _MSC_VER >= 1000
  11.  
  12. #if defined(_WIN32) && !defined(FASTCALL)
  13. #define FASTCALL __fastcall
  14. #endif
  15.  
  16. //
  17. // MIN/MAX/AVG functions, work best with basic types
  18. //
  19.  
  20. template < typename T >
  21. inline T FASTCALL MIN(const T a, const T b)
  22. {
  23.     return ((a < b) ? a : b);
  24. }
  25.  
  26. template < typename T >
  27. inline T FASTCALL MIN(const T a, const T b, const T c)
  28. {
  29.     return (MIN(MIN(a, b), c));
  30. }
  31.  
  32. template < typename T >
  33. inline T FASTCALL MAX(const T a, const T b)
  34. {
  35.     return ((a > b) ? a : b);
  36. }
  37.  
  38. template < typename T >
  39. inline T FASTCALL MAX(const T a, const T b, const T c)
  40. {
  41.     return (MAX(MAX(a, b), c));
  42. }
  43.  
  44. template < typename T >
  45. inline T FASTCALL AVG(const T a, const T b)
  46. {
  47.     return ((a + b) / 2);
  48. }
  49.  
  50. //
  51. // MIN/MAX/AVG functions, work best with user-defined types
  52. // (hopefully the compiler will choose the right one in most cases
  53. //
  54.  
  55.  
  56. template < typename T >
  57. inline T FASTCALL MIN(const T& a, const T& b)
  58. {
  59.     return ((a < b) ? a : b);
  60. }
  61.  
  62. template < typename T >
  63. inline T FASTCALL MIN(const T& a, const T& b, const T& c)
  64. {
  65.     return (MIN(MIN(a, b), c));
  66. }
  67.  
  68. template < typename T >
  69. inline T FASTCALL MAX(const T& a, const T& b)
  70. {
  71.     return ((a > b) ? a : b);
  72. }
  73.  
  74. template < typename T >
  75. inline T FASTCALL MAX(const T& a, const T& b, const T& c)
  76. {
  77.     return (MAX(MAX(a, b), c));
  78. }
  79.  
  80. template < typename T >
  81. inline T FASTCALL AVG(const T& a, const T& b)
  82. {
  83.     return ((a + b) / 2);
  84. }
  85.  
  86.  
  87. //
  88. // Generic Array Operations
  89. //
  90.  
  91. template < typename T >
  92. inline T FASTCALL MIN(const T* array, const int size)
  93. {
  94.     assert(size);
  95.     T               val = array[0];
  96.  
  97.     for (int i = 1; i < size; i++)
  98.     {
  99.         if (val > array[i])
  100.         {
  101.             val = array[i];
  102.         }
  103.     }
  104.     return val;
  105. }
  106.  
  107. template < typename T >
  108. inline T FASTCALL MAX(const T* array, const int size)
  109. {
  110.     assert(size);
  111.     T               val = array[0];
  112.  
  113.     for (int i = 1; i < size; i++)
  114.     {
  115.         if (val < array[i])
  116.         {
  117.             val = array[i];
  118.         }
  119.     }
  120.     return val;
  121. }
  122.  
  123. template < typename T >
  124. inline T FASTCALL AVG(const T* array, const int size)
  125. {
  126.     assert(size);
  127.     T               sum = array[0];
  128.  
  129.     for (int i = 1; i < size; i++)
  130.     {
  131.         sum += array[i];
  132.     }
  133.     return sum / num;
  134. }
  135.  
  136. template < typename T >
  137. inline T FASTCALL SUM(const T* array, const int size)
  138. {
  139.     assert(size);
  140.     T               sum = array[0];
  141.  
  142.     for (int i = 1; i < size; i++)
  143.     {
  144.         sum += array[i];
  145.     }
  146.     return sum;
  147. }
  148.  
  149.  
  150. // Uses one temp to swap, works best with user-defined types or doubles/long doubles
  151. template < typename T >
  152. inline void FASTCALL SWAP(T& a, T& b)
  153. {
  154.     T               temp = a;
  155.  
  156.     a = b;
  157.     b = temp;
  158. }
  159.  
  160.  
  161. // XOR math to swap (no temps), works with integral types very well
  162. template < typename T >
  163. inline void FASTCALL XOR_SWAP(T& a, T& b)
  164. {
  165.     a ^= b;
  166.     b ^= a;
  167.     a ^= b;
  168. }
  169.  
  170.  
  171. // Uses two temps to swap, works very well with built-in types on pipelined CPUs
  172. template < typename T >
  173. inline void FASTCALL PIPE_SWAP(T& a, T& b)
  174. {
  175.     T               tmpA = a;
  176.     T               tmpB = b;
  177.  
  178.     b = tmpA;
  179.     a = tmpB;
  180. }
  181.  
  182.  
  183. #endif // BASEMATH_H__
  184.